package com.lmf.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmf.eduservice.client.VodClient;
import com.lmf.eduservice.entity.EduVideo;
import com.lmf.eduservice.mapper.EduVideoMapper;
import com.lmf.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmf.resultCode.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author lmf
 * @since 2022-01-25
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    //将远程调用客户端注入进来
    @Autowired
    private VodClient vodClient;

    //1.根据课程id，删除课程里面的小节
    @Override
    public void removeVideoByCourseId(String courseId) {
        //0.根据课程id，删除所有的小节视频
        //获取所有的小节表中课程id，对应的所有视频真实id
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.select("video_source_id");
        List<EduVideo> eduVideoList = this.list(wrapper);
        //创建一个list集合封装所有的视频真实id
        List<String> videoIdList = new ArrayList<>();
        for (EduVideo eduVideo : eduVideoList) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if (videoSourceId != null) {
                //不为null再加入
                videoIdList.add(videoSourceId);
            }
        }
        //调用远程客户端的批量删除方法，删除所有视频
        if (!StringUtils.isEmpty(videoIdList)) {
            //集合不为null，再执行
            vodClient.deleteBatchVideo(videoIdList);
        }

        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id",courseId);
        this.remove(wrapper2);
    }
}
